<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>文章列表</title>
    <link rel="stylesheet" href="./libs/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/reset.css">
    <link rel="stylesheet" href="./css/iconfont.css">
    <link rel="stylesheet" href="./css/main.css">
    <script src="./libs/jquery/jquery.min.js"></script>
    <link rel="stylesheet" href="./libs/layui/css/layui.css">
    <script src="./libs/layui/layui.js"></script>
</head>

<body>
    <div class="container-fluid">
        <div class="common_title">
            文章列表
        </div>
        <div class="container-fluid common_con">
            <div class="row opt_btns">
                <div class="col-xs-6">
                    <div class="form-inline">
                        <input type="text" id="aname" class="form-control input-sm" placeholder="输入文章标题...">
                        <select id="selCategory" name="" class="form-control input-sm">
                            <option value="">所有分类</option>
                            <!-- <option>未分类</option>
                            <option>奇趣事</option>
                            <option>会生活</option>
                            <option>爱旅行</option> -->
                        </select>
                        <select id="selStatus" name="" class="form-control input-sm">
                            <option value="">所有状态</option>
                            <option value="草稿">草稿</option>
                            <option value="已发布">已发布</option>
                        </select>
                        <button id="btnSearch" class="btn btn-default btn-sm">筛选</button>
                    </div>
                </div>
                <div class="col-xs-6">
                    <a href="article_add.html" class="btn btn-success btn-sm pull-right" id="release_btn">发表文章</a>
                </div>
            </div>
            <table class="table table-striped table-bordered table-hover mp20">
                <thead>
                    <tr>
                        <th class="text-center">标题</th>
                        <th class="text-center">作者</th>
                        <th class="text-center">分类</th>
                        <th class="text-center">发表时间</th>
                        <th class="text-center">状态</th>
                        <th class="text-center" width="100">操作</th>
                    </tr>
                </thead>
                <tbody id="tbody">
                    <tr>
                        <td>王积千造统最头</td>
                        <td>杰克</td>
                        <td>奇趣事</td>
                        <td class="text-center">2017-06-08 07:08:46</td>
                        <td class="text-center">已发布</td>
                        <td class="text-center">
                            <a href="article_edit.html" class="btn btn-default btn-xs">编辑</a>
                            <a href="javascript:void(0);" class="btn btn-danger btn-xs delete">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
            <div class="row text-center" id="test1">
            </div>
        </div>
    </div>


    <script src="./libs/art-template/template-web.js"></script>
    <!-- 定义文章分类列表 -->
    <script id="art_cate" type="text/html">
        {{each data}}
<option value="{{$value.id}}">{{$value.name}}</option>
        {{/each}}
    </script>


    <!-- 定义文章列表模板 -->
    <script id="art_list" type="text/html">
    {{each data}}
    <tr>  
        <td>{{$value.title}}</td>
        <td class='text-center'>{{$value.author}}</td>
        <td class='text-center'>{{$value.category}}</td>
        <td class='text-center'>{{$value.date}}</td>
        <td class='text-center'>{{$value.state}}</td>
        <td class='text-center'>
            <a href="article_edit.html?id={{$value.id}}" class="btn btn-default btn-xs">编辑</a>
            <a href="javascript:void(0)"  onclick="del({{$value.id}})" class="btn btn-danger btn-xs delete">删除</a>
        </td>
    </tr>
  {{/each}}
    </script>
    <script src="./js/http.js"></script>
    <script>
        $(function () {

            get_list();

            get_cate_list();


            // render_pager();

            $('#btnSearch').click(function () {
                get_list();
            })

        })
        //1.0
        function get_list(curr, perpage) {
            let parms = {
                key: $('#aname').val(), //文章关键字
                type: $('#selCategory').val(),//文章的分类
                state: $('#selStatus').val(),//文章的发布状态
                page: curr,//当前页码
                perpage: perpage,//当前分页要显示的数据条数,默认6条
            }

            // 1.0获取列表数据到页面
            myAjax('get', 'http://localhost:8080/api/v1/admin/article/query', parms, function (resData) {
                console.log(resData);
                console.log(resData.data);



                let resHtml = template('art_list', resData.data);
                // console.log(resHtml);
                $('#tbody').html(resHtml);
                // console.log(resData)

                //生成数据表后立刻调用分页码及生成分页码
                render_pager(resData.data.totalCount, curr);
            });
        }


        //2.0获取文章分类
        function get_cate_list() {

            myAjax('get', 'http://localhost:8080/api/v1/admin/category/list', null, function (resData) {
                console.log(resData);


                let opthtml = template('art_cate', resData);
                $('#selCategory').append(opthtml);
            })
        }


        // 3.0分页码
        //生成分页控件
        function render_pager(totalCount, currpage) {
            layui.use('laypage', function () {
                //获取到分页控件对象
                var laypage = layui.laypage;
                //执行一个laypage实例
                laypage.render({
                    elem: 'test1' //注意，这里的 test1 是 ID，不用加 # 号
                    , count: totalCount, //数据总数，从服务端得到
                    limit: 6,   //每页显示条数
                    curr: currpage, //当前页码
                    jump: function (obj, first) {
                        // alert(first)
                        //obj包含了当前分页的所有参数.ex:(cyrr:得到当前页,limit:得到每页显示的条数)
                        if (!first) {
                            get_list(obj.curr, obj.limit);
                        }

                    }
                });
            })

        }













        //删除按钮
        function del(a_id) {
            // alert(a_id);
            myAjax('post', 'http://localhost:8080/api/v1/admin/article/delete', { id: a_id }, function (resData) {
                if (resData.code !== 200) {
                    alert('删除失败');
                    return;
                }
                get_list();
            })

        }
    </script>
</body>

</html>